#!/bin/bash

E_MC_INLCUDE_ARGSLIB=65
E_MC_INLCUDE_ARGSLIB_MSG="Impossibile includere argslib.sh"
E_MC_INLCUDE_LOGLIB=66
E_MC_INLCUDE_LOGLIB_MSG="Impossibile includere loglib.sh"

# FFC is for FILES_FOR_CLIENT
FFC_BASEPATH=./src
FFC_NEEDED_BASH=( bash/argslib.sh bash/loglib.sh )
FFC_NEEDED_COMMON=( src/io-client.properties \
	src/logger-client.properties \
	src/client-start \
	src/keystore )
FFC_NEEDED_JAVA=( src/io/IOColor.java \
	src/io/Logger.java \
	src/io/LoggerConstants.java \
	src/io/LoggerLevel.java \
	src/io/LoggerProperties.java \
	src/io/Pair.java \
	src/io/PropertyManager.java \
	src/io/IO.java \
	src/io/IOProperties.java \
	src/io/IOConstants.java \
	src/io/exceptions/InOutException.java \
	src/io/exceptions/ListIsEmptyException.java \
	src/clientbootstrap/ClientBootstrap.java \
	src/plog/PLog.java )
FFC_NEEDED_JAVA_BIN=( src/io/IOColor.class \
	src/io/Logger.class \
	src/io/LoggerConstants.class \
	src/io/LoggerLevel.class \
	src/io/LoggerProperties.class \
	src/io/Pair.class \
	src/io/PropertyManager.class \
	src/io/IO.java \
	src/io/IOProperties.java \
	src/io/IOConstants.java \
	src/io/exceptions/InOutException.class \
	src/io/exceptions/ListIsEmptyException.class \
	src/clientbootstrap/ClientBootstrap.class \
	src/plog/PLog.class )
FFC_TAR=ffc-needed.tar
FFC_PATH=micro-client

LOG_POSITION=$(basename $0)

# include argslib.sh
. ./bash/argslib.sh 2> /dev/null
[[ $? -gt 0 ]] && echo $E_MC_INLCUDE_ARGSLIB_MSG && exit $E_MC_INLCUDE_ARGSLIB

# include loglib.sh
. ./bash/loglib.sh 2> /dev/null
[[ $? -gt 0 ]] && echo $E_MC_INLCUDE_LOGLIB_MSG && exit $E_MC_INLCUDE_LOGLIB

G_UCLIENT_NAME="uclient/get-uclient"
G_LOGPOS=$(basename $0)

function copy_needed_files_src()
{
	tar cfp $FFC_TAR ${FFC_NEEDED_JAVA[@]}
	tar rfp $FFC_TAR ${FFC_NEEDED_BASH[@]}
	tar rfp $FFC_TAR ${FFC_NEEDED_COMMON[@]}
}

function copy_needed_files_bin()
{
	tar cfp $FFC_TAR ${FFC_NEEDED_JAVA_BIN[@]}
	tar rfp $FFC_TAR ${FFC_NEEDED_BASH[@]}
	tar rfp $FFC_TAR ${FFC_NEEDED_COMMON[@]}
}

function mv_common_files()
{
	mv -f src/io.properties ./
	mv -f src/logger-client.properties ./
}

bt_info "copy_needed_files_src" "creo l'archivio $FFC_TAR" $G_LOGPOS
bt_info "mkdir -p $FFC_PATH" "creo la directory $FFC_PATH" $G_LOGPOS
bt_info "mv $FFC_TAR $FFC_PATH/" "sposto l'archivio $FFC_TAR" $G_LOGPOS
cd $FFC_PATH
bt_info "tar xf $FFC_TAR" "estraggo il microclient" $G_LOGPOS
#bt_info "mv_common_files" "riorganizzo i files" $G_LOGPOS
bt_info "rm -rf $FFC_TAR" "pulisco l'ambiente" $G_LOGPOS
cd ..


bt_info "touch $G_UCLIENT_NAME" "touch $G_UCLIENT_NAME" $G_LOGPOS

bt_info_up "write headers to $G_UCLIENT_NAME" $G_LOGPOS
echo '#!/bin/bash' > $G_UCLIENT_NAME 2> /dev/null
bt_info_down $BT_TYPE_EQ_ZERO $? 

bt_info_up "write argslib.sh to $G_UCLIENT_NAME" $G_LOGPOS
cat ./bash/argslib.sh >> $G_UCLIENT_NAME 2> /dev/null
bt_info_down $BT_TYPE_EQ_ZERO $? 

bt_info_up "write loglib.sh to $G_UCLIENT_NAME" $G_LOGPOS
cat ./bash/loglib.sh >> $G_UCLIENT_NAME 2> /dev/null
bt_info_down $BT_TYPE_EQ_ZERO $? 

bt_info_up "write download-microclient code to $G_UCLIENT_NAME" $G_LOGPOS
cat ./download-microclient >> $G_UCLIENT_NAME 2> /dev/null
bt_info_down $BT_TYPE_EQ_ZERO $? 

bt_info "chmod +x $G_UCLIENT_NAME" "set permission to +x for $G_UCLIENT_NAME" $G_LOGPOS

